/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WaitDialog.java
 *
 * Created on 28.5.2011, 8:54:52
 */

package src.clovece;

import java.util.logging.Level;
import java.util.logging.Logger;
import src.clovece.game.GameInfo;
import src.clovece.game.Multiplayer;

/**
 *
 * @author Thomas
 */
public class WaitDialog extends javax.swing.JDialog {

    GameInfo gameInfo = GameInfo.getGameInfoInstance();
    Multiplayer mp = new Multiplayer();
    /** Creates new form WaitDialog */
    public WaitDialog(GameFrame parent, boolean modal) {
        super(parent, modal);
        this.setResizable(false);
        this.setLocation(parent.getX()+400, parent.getY()+200);
        this.initComponents();
    }

    public void writeMessagge(String msg) {
        this.TextArea.setText(this.TextArea.getText() + "\n" + msg);
        System.out.println(msg);
    }

    public void waitMessage()
    {
        String message = null;
        message = mp.readMessage();

        if (message.equals("PlayersJoined"))
        {
          this.writeMessagge("All players have joined the game.");
          for(int i=0; i<this.gameInfo.getNumberOfPlayers();i++) {
              this.gameInfo.setPlayerNickname(mp.readMessage(), i);
              this.writeMessagge(this.gameInfo.getPlayerNickname(i));
          }
          int[] array = new int[6];
          for(int i=0; i<6;i++) {
              array[i] = Integer.parseInt(mp.readMessage());
          }
          this.gameInfo.setGeneratedValues(array);
          this.dispose();
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NameLabel = new javax.swing.JLabel();
        ScrollPane = new javax.swing.JScrollPane();
        TextArea = new javax.swing.JTextArea();
        HistoryLabel = new javax.swing.JLabel();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        NameLabel.setFont(new java.awt.Font("Tahoma", 1, 18));
        NameLabel.setText("Wating for other players ...");

        TextArea.setColumns(20);
        TextArea.setFont(new java.awt.Font("Times New Roman", 0, 12));
        TextArea.setRows(5);
        TextArea.setFocusable(false);
        ScrollPane.setViewportView(TextArea);

        HistoryLabel.setText("Log:");

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(HistoryLabel)
                            .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(121, 121, 121)
                        .addComponent(CancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(NameLabel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(NameLabel)
                .addGap(20, 20, 20)
                .addComponent(HistoryLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CancelButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        final WaitDialog wd = this;
        Runnable r = new Runnable() {
            public void run() {
                wd.waitMessage();
            }
        };
        new Thread(r).start();
    }//GEN-LAST:event_formWindowActivated

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel HistoryLabel;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JScrollPane ScrollPane;
    private javax.swing.JTextArea TextArea;
    // End of variables declaration//GEN-END:variables

}
